import { ColumnConfiguration, RelationConfiguration, SeedConfiguration } from "@/views/dev-types"
import { getSeed } from "@/views/seed-dev"
import { cloneDeep, camelCase, snakeCase, lowerFirst } from "lodash-es"
import { getNameColumn } from "./SeedUtil"

export function formatDeclare(seed: SeedConfiguration, declare: string, isNameColumn = false) {
  const { columns } = seed
  if(columns[declare]) return [declare]

  const ret: string[] = []
  if(declare.includes('.')) {
    const pos = declare.indexOf('.')
    const cross = declare.substring(0, pos)
    const subFullColumn = declare.substring(pos + 1)
    const relation = getRelation(seed, cross)
    const subDeclares = formatDeclare(getSeed(relation.seedName), subFullColumn)
    for(const subDeclare of subDeclares) ret.push(`${cross}.${subDeclare}`)
  } else {
    const relation = getRelation(seed, declare)
    if(relation) {
      const relationSeed = getSeed(relation.seedName)
      if(isNameColumn) {
        const nameColumn = getNameColumn(relationSeed)
        ret.push(`${declare}.${nameColumn}`)
      } else {
        for(const columnName in relationSeed.columns) {
          if(relation.targetColumn === columnName || 'id' === columnName) continue
          ret.push(`${declare}.${columnName}`)
        }
      }
    }
  }

  return ret
}

export function formatDeclares(seed: SeedConfiguration, declares: string[], isNameColumn = false) {
  const ret: string[] = []
  for(const declare of declares) ret.push(...formatDeclare(seed, declare, isNameColumn))

  return Array.from(new Set(ret))
}

export function getRawColumns(seed: SeedConfiguration, declare: string): Record<string, ColumnConfiguration> {
  const { columns } = seed
  if(columns[declare]) return {[declare]: cloneDeep(columns[declare])}

  return getCrossColumns(seed, declare)
}

export function getCrossColumns(seed: SeedConfiguration, fullCross: string): Record<string, ColumnConfiguration> {
  let cross = fullCross
  let subFullColumn = void 0
  if(fullCross.includes('.')) {
    const pos = fullCross.indexOf('.')
    cross = fullCross.substring(0, pos)
    subFullColumn = fullCross.substring(pos + 1)
  }

  const relation = getRelation(seed, cross)
  return getCrossDeepColumns(cross, relation, subFullColumn)
}

export function getCrossDeepColumns(parentPath: string, parentRelation: RelationConfiguration, fullColumn: string) {
  const seed = getSeed(parentRelation.seedName)
  console.log('parentRelation==', parentRelation)
  const parentLabel = parentRelation.label || parentRelation.comment
  if(!fullColumn) {
    const ret: Record<string, ColumnConfiguration> = {}
    for(const columnName in seed.columns) {
      const column = seed.columns[columnName]
      ret[`${parentPath}.${columnName}`] = {...cloneDeep(column), label: `${parentLabel}.${column.label}`}
    }

    return ret
  }

  if(fullColumn.includes('.')) {
    const pos = fullColumn.indexOf('.')
    const cross = fullColumn.substring(0, pos)
    const subFullColumn = fullColumn.substring(pos + 1)
    const relation = getRelation(seed, cross)
    return getCrossDeepColumns(`${parentPath}.${cross}`, relation, subFullColumn)
  } else {
    const { columns } = seed
    if(columns[fullColumn]) {
      return {[`${parentPath}.${fullColumn}`]: {...columns[fullColumn], label: `${parentLabel}.${columns[fullColumn].label}`}}
    }

    const relation = getRelation(seed, fullColumn)
    return getCrossDeepColumns(`${parentPath}.${fullColumn}`, relation, void 0)
  }
}

// export function getCrossDeepColumn(parentPath: string, parentRelation: RelationConfiguration, fullColumn: string) {
//   const seed = getSeed(parentRelation.seedName)
//   const parentLabel = parentRelation.label || parentRelation.comment
//   if(!fullColumn) {
//     let columns = seed.columns
//     const nameColumn = snakeCase(seed.nameColumn)
//     if(nameColumn) {
//       if(!columns[nameColumn]) throw new Error(`【${seed.name}】下【nameColumn】配置错误，列【${nameColumn}】不存在`)

//       return { ...columns[nameColumn], name: `${parentPath}.${nameColumn}`, label: `${parentLabel}${columns[nameColumn].label}` }
//     }
    
//     if(columns['name']) return { ...columns['name'], name: `${parentPath}.name`, label: `${parentLabel}${columns['name'].label}` }
//     if(columns['id']) return { ...columns['id'], name: `${parentPath}.id`, label: `${parentLabel}${columns['id'].label}` }

//     throw new Error(`请为【${seed.name}】配置正确的【nameColumn】`)
//   }

//   if(fullColumn.includes('.')) {
//     const pos = fullColumn.indexOf('.')
//     const cross = fullColumn.substring(0, pos)
//     const subFullColumn = fullColumn.substring(pos + 1)
//     const relation = getRelation(seed, cross)
//     return getCrossDeepColumn(parentPath + '.' + camelCase(cross), relation, subFullColumn)
//   } else {
//     const { columns } = seed
//     if(columns[fullColumn]) return { ...columns[fullColumn], name: `${parentPath}.${fullColumn}`, label: `${parentLabel}${columns[fullColumn].label}` }

//     const relation = getRelation(seed, fullColumn)
//     return getCrossDeepColumn(parentPath + '.' + camelCase(fullColumn), relation, undefined)
//   }
// }

export function getRelation(seed: SeedConfiguration, cross: string) {
  cross = snakeCase(cross)
  const { hasOne, belongsTo, hasMany, habtm } = seed
  if(hasOne && hasOne[cross]) return hasOne[cross]
  if(belongsTo && belongsTo[cross]) return belongsTo[cross]
  if(hasMany && hasMany[cross]) return hasMany[cross]
  if(habtm && habtm[cross]) return habtm[cross]

  throw new Error(`【${seed.name}】下的关系【${cross}】不存在`)
}